using System;
using System.Web;
using System.Net;
using System.IO;
using System.Text;
using System.Collections.Generic;

namespace Utils
{
public class JsonUtil
{
	/*
     * This method takes in JSON in the form returned by javascript's
     * JSON.stringify(Object) and returns a string->string dictionary.
     * This method may be of use when the format of the json is unknown.
     * You can modify the delimiters, etc pretty easily in the source
     * (sorry I didn't abstract it--I have a very specific use).
     */ 
	public static Dictionary<string, string> jsonParse(string rawjson)
	{
        Dictionary<string, string> outdict = new Dictionary<string, string>();
        StringBuilder keybufferbuilder = new StringBuilder();
        StringBuilder valuebufferbuilder = new StringBuilder();
        StringReader bufferreader = new StringReader(rawjson);

        int s = 0;
        bool reading = false;
        bool inside_string = false;
        bool reading_value = false;
        //break at end (returns -1)
        while (s >= 0)
        {
            s = bufferreader.Read();
            //opening of json
            if (!reading)
            {
                if ((char)s == '{' && !inside_string && !reading) reading = true;
                continue;
            }
            else
            {
                //if we find a quote and we are not yet inside a string, advance and get inside
                if (!inside_string)
                {
                    //read past the quote
                    if ((char)s == '\"') inside_string = true;
                    continue;
                }
                if (inside_string)
                {
                    //if we reached the end of the string
                    if ((char)s == '\"')
                    {
                        inside_string = false;
                        s = bufferreader.Read(); //advance pointer
                        if ((char)s == ':')
                        {
                            reading_value = true;
                            continue;
                        }
                        if (reading_value && (char)s == ',')
                        {
                            //we know we just ended the line, so put itin our dictionary
                            if (!outdict.ContainsKey(keybufferbuilder.ToString())) outdict.Add(keybufferbuilder.ToString(), valuebufferbuilder.ToString());
                            //and clear the buffers
                            keybufferbuilder.Remove(0,keybufferbuilder.Length);//Clear();
                            valuebufferbuilder.Remove(0,valuebufferbuilder.Length);//Clear();
                            reading_value = false;
                        }
                        if (reading_value && (char)s == '}')
                        {
                            //we know we just ended the line, so put itin our dictionary
                            if (!outdict.ContainsKey(keybufferbuilder.ToString())) outdict.Add(keybufferbuilder.ToString(), valuebufferbuilder.ToString());
                            //and clear the buffers
                            keybufferbuilder.Remove(0,keybufferbuilder.Length);//Clear();
                            valuebufferbuilder.Remove(0,valuebufferbuilder.Length);//Clear();
                            reading_value = false;
                            reading = false;
                            break;
                        }
                    }
                    else
                    {
                        if (reading_value)
                        {
                            valuebufferbuilder.Append((char)s);
                            continue;
                        }
                        else
                        {
                            keybufferbuilder.Append((char)s);
                            continue;
                        }
                    }
                }
                else
                {
                    switch ((char)s)
                    {
                        case ':':
                            reading_value = true;
                            break;
                        default:
                            if (reading_value)
                            {
                                valuebufferbuilder.Append((char)s);
                            }
                            else
                            {
                                keybufferbuilder.Append((char)s);
                            }
                            break;
                    }
                }
            }
        }
        return outdict;
    }

	public static List<UserInfo> parseUserInfo(string json)
	{
		List<UserInfo> users = new List<UserInfo>();
		int i = 0, i2 = 0, i3 = 0;
		while((i=json.IndexOf("id", i+1)) > 0)
		{
			i2 = json.IndexOf(":",i);
			i3 = json.IndexOf(",",i);
			string id = json.Substring(i2, i3 - i2);

			i=json.IndexOf("username", i+1);
			i2 = json.IndexOf(":",i);
			i3 = json.IndexOf(",",i);
			string name = json.Substring(i2, i3 - i2);

			UserInfo ui = new UserInfo();
			ui.id = id.Trim(" \":".ToCharArray());
			ui.username = name.Trim(" \":".ToCharArray());
			users.Add(ui);
		}
		return users;
	}

	public class UserInfo
	{
		public string id;
		public string username;
	}
}
}